import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import axios from 'axios'
import VueAxios from 'vue-axios'
import Vuex from 'vuex'
import VueCookies from 'vue-cookies'

import home from './pages/home'
import add from './pages/add'
import search from './pages/search'
import detail from './pages/detail'
import validate from './pages/validate'

Vue.config.productionTip = false

Vue.use(ElementUI)

Vue.use(Vuex)
Vue.use(VueAxios, axios)
Vue.use(VueRouter)
Vue.use(VueCookies)


const router = new VueRouter({
    base: '',
    mode: "history",
    routes: [
        {
            name: 'home',
            path: '/home',
            meta: {
                title: '星火商店'
            },
            component: home
        },
        {
            name: 'validate',
            path: '/validate',
            meta: {
                title: '星火商店'
            },
            component: validate
        },
        {
            name: 'search',
            path: '/search', 
            meta: {
                // 页面标题title
                title: '星火商店'
            },
            component: search
        },
        {
            name: 'add',
            path: '/add', 
            meta: {
                // 页面标题title
                title: '星火商店'
            },
            
            component: add
        },
        {
            name: 'detail',
            path: '/detail/:id', 
            meta: {
                // 页面标题title
                title: '星火商店'
            },
            component: detail
        },
        {
            path: '/',
            redirect: '/home'
        },
        {
            path: '*',
            redirect: '/home'
        }
    ]
})

router.beforeEach((to, from, next) => {
    window.document.title = '星火商店'
    next()
})

const store = new Vuex.Store({
    state: {
        categoryList: [],
        commitForm: {
            deb_name: "",
            version: "",
            file_name: "",
            package_name: "",
            author: "",
            uploader: "",
            website: "",
            size: "",
            desc: "",
            logo: "",
            screen_shots: "",
            category_id: ""
        },
        adminForm: {
            admin_name: "",
            admin_pwd: "",
            admin_nickname: ""
        },
        screenshotsList: [],
        logoList: [],
        logoUrl: "",
        homePageDebList: [],
        homePageDebTotal: 0,
        currentCategoryIndex: 0,
        searchResult: [],
        latestDebList: []
    },
    getters: {
        getCategoryList: state => {
            return state.categoryList
        },
        getCommitForm: state => {
            return state.commitForm
        },
        getAllDebCache: state => {
            return state.homePageDebList.concat(state.searchResult).concat(state.latestDebList);
        }
    },
    mutations: {
        setLatestDebList(state, payload) {
            state.latestDebList = payload
        },
        setAdminForm(state, payload) {
            state.adminForm = payload
        },
        setSearchResult(state, payload) {
            state.searchResult = payload
        },
        setCurrentCategoryIndex(state, payload) {
            state.currentCategoryIndex = payload
        },
        setCategoryList(state, payload) {
            state.categoryList = payload
        },
        setHomePageDebList(state, payload) {
            state.homePageDebList = payload["list"]
            state.homePageDebTotal = payload["total"]
        },
        setImageUrl(state, payload) {
            state.logoUrl = payload
        },
        clearCommitForm(state) {
            state.logoList = [];
            state.screenshotsList = [];
            state.logoUrl = ""
            state.commitForm = {
                deb_name: "",
                version: "",
                file_name: "",
                package_name: "",
                author: "",
                uploader: "",
                website: "",
                size: "",
                desc: "",
                logo: "",
                screen_shots: "",
                category_id: ""
            }
        }
    }
})


new Vue({
    router,
    store,
    render: h => h(App),
}).$mount('#app')
